package com.tdd.mockexample;

public class LoginService {

	private final AccountRepoisitory repository;
	private String lastAccountName;
	private int failedAttempts;

	public LoginService(AccountRepoisitory repository) {
		this.repository = repository;
	}

	public void login(String accountName, String candidatePassword) {
		Account account = repository.findAccountNamed(accountName);

		if (account.isRevoked())
			throw new AccountRevokedException();

		if (account.passwordMatches(candidatePassword))
			account.setLoggedIn(true);
		else {
			if (accountName.equals(lastAccountName)) {
				++failedAttempts;
				if (failedAttempts > 2)
					account.setRevoked(true);
			} else {
				lastAccountName = accountName;
				failedAttempts = 1;
			}

		}
	}
}
